package com.cdut.service.serviceimp;

import com.cdut.dao.UserDao;
import com.cdut.pojo.User;
import com.cdut.service.UserService;
import com.cdut.util.JsonResponce;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceimp implements UserService {

    @Autowired
    UserDao userDao;

    /**
     * 实现用户的添加
     * @param user 用户对象
     * @return JsonResponce
     */
    @Override
    public JsonResponce UserAdd(User user) {
        System.err.println(user);
        int flag=1;
        if(userDao.queryUserByMail(user.getMail())==null&&userDao.queryUserByName(user.getUsername())==null)
        {
            flag=0;
        }
        if(flag==1)
        {
            return new JsonResponce(0,"操作失败",null);
        }
        int code=userDao.addUser(user);
        JsonResponce jsonResponce=new JsonResponce(code,"操作成功",null);
        if(code==0)
        {
            jsonResponce.setMsg("操作失败");
        }
        return jsonResponce;
    }

    /**
     * 实现通过uid删除用户信息
     * @param uid 用户id
     * @return JsonResponce
     */
    @Override
    public JsonResponce UserDeleteByuid(int uid) {
        int code=userDao.deleteUser(uid);
        JsonResponce jsonResponce=new JsonResponce(code,"操作成功",null);
        if(code==0)
        {
            jsonResponce.setMsg("操作失败");
        }
        return jsonResponce;
    }

    /**
     * 实现用户的信息维护
     * @param user 用户对象
     * @return JsonResponce
     */
    @Override
    public JsonResponce UserModifyByid(User user) {
        int code=userDao.upDateUser(user);
        JsonResponce jsonResponce=new JsonResponce(code,"操作成功",null);
        if(code==0)
        {
            jsonResponce.setMsg("操作失败");
        }
        return jsonResponce;
    }

    /**
     * 实现全部用户的查询
     * @param
     * @return JsonResponce<List<User>>
     */
    @Override
    public JsonResponce<List<User>> UserSerch() {
        List<User> userList=null;
        userList=userDao.queryUserList();
        JsonResponce jsonResponce = new JsonResponce<List<User>>(0, "操作成功", userList);
            if (!userList.isEmpty()) {
                jsonResponce.setCode(0);
                jsonResponce.setMsg("用户为空，操作失败");
            }
        return jsonResponce;
    }

    /**
     * 实现通过邮箱查询用户信息
     * @param mail 用户邮箱
     * @return JsonResponce
     */
    @Override
    public JsonResponce<User> UserSerchBymail(String mail) {
        User user=new User();
        user=userDao.queryUserByMail(mail);
        JsonResponce jsonResponce=new JsonResponce<User>(0,"操作成功",user);
        return jsonResponce;
    }

    /**
     * 实现用户的登录
     * @param user 用户全部的登录信息
     * @return JsonResponce
     */
    public JsonResponce UserLogin(User user)
    {
        User request=userDao.queryUserByMail(user.getMail());
        if(user.getUsername().equals(request.getUsername())&&user.getPassword().equals(request.getPassword()))
        {
            return new JsonResponce(1,"登录成功",null);
        }
        else {
            return new JsonResponce(0,"登录失败",null);
        }
    }

    /**
     * 通过用户名查询
     * @param username 用户名
     * @return json
     */
    @Override
    public JsonResponce<User> queryUserByName(String username){
        User user =new User();
        user = userDao.queryUserByName(username);

        if (user!=null){
            JsonResponce<User> userJsonResponce = new JsonResponce<>(1, "操作成功", user);
            return  userJsonResponce;
        }
        else {
            return  new JsonResponce<User>(0,"操作失败",user);
        }

    }

    /**
     * 通过id查询用户
     * @param id 用户id
     * @return json
     */
    @Override
    public JsonResponce<User> queryUserById(int id) {
        User user=userDao.queryUserById(id);
        if (user!=null){
            return new JsonResponce<>(1, "操作成功", user);
        }else{
            return new JsonResponce<User>(0,"操作失败",null);
        }
    }
}